// page/songDetail/songDetail.js
import request from "../../utils/request";
import moment from "moment";
import PubSub from 'pubsub-js'

const appInstance = getApp(); // 全局播放实列
let pid = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        songDetail: {},
        playId: '',
        isPlay: false,
        startTime: '00:00',
        endTime: '00:00',
        currentWith: 0,
        musicLink: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let playId = options.musicId;
        this.backgroundAudioManager = wx.getBackgroundAudioManager();
        // 判断当前页面音乐是否在播放
        if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === playId) {
            // 修改当前页面音乐播放状态为true
            this.changePlayState(true)
        } else {
            this.changePlayState(false)
        }
        // 监视音乐播放/暂停/停止
        this.backgroundAudioManager.onPlay(() => {
            this.changePlayState(true);
            // 修改全局音乐播放的状态
            appInstance.globalData.musicId = playId;
        });
        this.backgroundAudioManager.onPause(() => {
            this.changePlayState(false);
        });
        this.backgroundAudioManager.onStop(() => {
            this.changePlayState(false);
        });
        this.getSongDetails(playId);
        this.playMusic(playId);
        // 监听音乐实时播放的进度
        this.backgroundAudioManager.onTimeUpdate(() => {
            // 格式化实时的播放时间
            let startTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
            let currentWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 450;
            this.setData({
                startTime,
                currentWidth
            })
        })
    },
    // 切歌
    switchSong(event) {
        let type = event.currentTarget.id;
        if (type === "pre") {
            PubSub.publish("switchType", "pre");
        } else if (type === "post") {
            PubSub.publish("switchType", "post");
        } else {
            return;
        }
        PubSub.subscribe("musicId", (id, data) => {
            this.backgroundAudioManager.stop();
            this.getSongDetails(data)
            this.playMusic(data);
        });
        PubSub.unsubscribe();
    },
    // 获取歌曲详情
    async getSongDetails(ids) {
        let songDetailData = await request("/song/detail", {ids});
        this.setData({
            songDetail: songDetailData.songs[0],
            endTime: moment(songDetailData.songs[0].dt).format("mm:ss")
        });
        wx.setNavigationBarTitle({
            title: this.data.songDetail.name
        });
    },
    playOrStop() {
        if (!this.data.isPlay) {
            this.backgroundAudioManager.play();
            this.changePlayState(true)
        } else {
            this.backgroundAudioManager.pause();
            this.changePlayState(false)
        }
    },
    async playMusic(ids) {
        let resultData = await request("/song/url", {id: ids});
        this.backgroundAudioManager.title = resultData.data[0].name;
        this.backgroundAudioManager.src = resultData.data[0].url;
        this.setData({
            playId: ids,
            isPlay: true,
        });
    },
    // 改变播放状态
    changePlayState(isPlay) {
        this.setData({
            isPlay
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
